package com.oros.db;
import java.util.LinkedHashMap;

public class AEGeneratedListData implements AEGeneratedData,AEApplyTranslation {
	    private static final long serialVersionUID = -1631631846242881120L;
		private String columnName;
        private LinkedHashMap<?,?> map,origMap;
        private int colno=-1;
	public AEGeneratedListData(String columnName,LinkedHashMap<?,?> map,AETranslation translation)
	{
		this.columnName = columnName;
		this.map = map;
		this.origMap = translation==null?null:map;
		if (translation!=null) applyTranslation(translation);
	}
	public AEGeneratedListData(String columnName,LinkedHashMap<?,?> map)
	{
		this(columnName,map,null);
	}
	@Override
	public void applyTranslation(AETranslation translation)
	{
		if (translation==null || origMap==null) return;
		LinkedHashMap<Object,Object> vmap = new LinkedHashMap<Object,Object>(origMap.size());
		for(Object key:origMap.keySet())
		{
			String val=(String)origMap.get(key);
			if (val!=null)
			{
				String nval=translation.getTranslationSoft(val);
				if (nval!=null) val=nval;
			}
            vmap.put(key, (Object)val); 			
		}
		map = vmap;
	}
	@Override
	public Object generateCell(AETableContainer table, AERawItem item,int columnNo,int action) {
		if (colno<0)
		{
			colno = table.getColumnIndex(columnName);
			if (colno<0)
				throw new RuntimeException("AEGeneratedListData: Column "+columnName+" is not present in table "+table);
		}
		Object a= item.getColumnValue(colno);
		return map.get(a);
	}

}
